package com.sh.rbac.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sh.rbac.domain.dto.jobLog.JobLogQueryDTO;
import com.sh.rbac.domain.entity.JobLog;
import com.sh.rbac.mapper.JobLogMapper;
import com.sh.rbac.service.JobLogService;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Service;

/**
 * @author lucky
 * @description 针对表【sys_job_log】的数据库操作Service实现
 * @createDate 2024-09-02 14:36:31
 */
@Service
public class JobLogServiceImpl extends ServiceImpl<JobLogMapper, JobLog>
        implements JobLogService {

    @Override
    public Page<JobLog> getPage(Long current, Long limit, JobLogQueryDTO jobLogQueryDTO) {
        Page<JobLog> page = new Page<>(current, limit);

        // 查询参数
        String jobName = jobLogQueryDTO.getJobName();
        String jobGroup = jobLogQueryDTO.getJobGroup();
        Integer status = jobLogQueryDTO.getStatus();
        String startTime = null;
        String endTime = null;
        String[] execTimes = jobLogQueryDTO.getExecTimes();

        if (execTimes != null) {
            startTime = execTimes[0];
            endTime = execTimes[1];
        }

        // 构造条件
        LambdaQueryWrapper<JobLog> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(jobName != null, JobLog::getJobName, jobName)
                .like(jobGroup != null, JobLog::getJobGroup, jobGroup)
                .eq(status != null, JobLog::getStatus, status)
                .between(startTime != null && endTime != null, JobLog::getExecTime, startTime, endTime);

        // 根据创建时间排序
        wrapper.orderByDesc(JobLog::getCreateTime);

        // 返回查询结果
        return page(page, wrapper);
    }


    @Override
    public void exportData(Integer startCount, Integer endCount, HttpServletResponse response) {

    }
}




